
import { Handler, definePlugin, param, Types } from 'hydrooj';


class SanhaiRedirectHandler extends Handler {
    async get() {

        const currentPath = this.request.path;
        const query = this.request.querystring ? `?${this.request.querystring}` : '';


        const newUrl = `https://sanhaioj.longzhuge.com.cn${currentPath}${query}`;


        this.response.redirect = newUrl;


        this.response.template = 'redirect.html';
        this.response.body = { redirect_url: newUrl };
    }
}


class StaticHandler extends Handler {
    async get() {
        // 获取请求的文件路径
        const filePath = this.request.path.replace('/addon/sanhai-redirect/', '');

        // 根据文件路径返回对应的内容
        if (filePath === 'redirect.css') {
            this.response.type = 'text/css';
            // 实际项目中应该从文件系统读取
            // 这里简化处理，直接返回CSS内容
            this.response.body = `/* 跳转页面样式 */
.redirect-container {
    max-width: 600px;
    margin: 0 auto;
    padding: 2rem;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
    text-align: center;
    position: relative;
    overflow: hidden;
}

.redirect-container::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 5px;
    background: linear-gradient(90deg, #4e73df, #36b9cc);
}

.spinner {
    width: 3rem;
    height: 3rem;
    margin-bottom: 1rem;
    border-width: 0.25em;
}

.countdown {
    font-weight: bold;
    color: #4e73df;
    font-size: 1.5rem;
}

.redirect-url {
    font-family: monospace;
    background-color: #f8f9fa;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    word-break: break-all;
    margin: 1rem 0;
    display: inline-block;
}

.btn-primary {
    background-color: #4e73df;
    border-color: #4e73df;
    padding: 0.75rem 1.5rem;
    font-weight: 500;
    transition: all 0.2s;
}

.btn-primary:hover {
    background-color: #2e59d9;
    border-color: #2653d4;
    transform: translateY(-1px);
}

.progress {
    height: 5px;
    margin-top: 1.5rem;
    background-color: #e9ecef;
}

.progress-bar {
    transition: width 1s linear;
}

/* 响应式调整 */
@media (max-width: 576px) {
    .redirect-container {
        margin: 1rem;
        padding: 1.5rem;
    }

    .redirect-url {
        font-size: 0.9rem;
    }
}

/* 添加动画效果 */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.redirect-container {
    animation: fadeIn 0.5s ease-out;
}

/* 添加脉冲效果 */
@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

.redirect-container:hover {
    animation: pulse 0.5s ease-in-out;
}`;
        }
    }
}

// 插件初始化函数
export async function apply(ctx) {
    // 注册一个路由，匹配所有路径
    // 使用通配符路由匹配所有可能的请求
    ctx.Route('sanhai_redirect', '/(.*)', SanhaiRedirectHandler);

    // 注册静态资源路由
    ctx.Route('sanhai_static', '/addon/sanhai-redirect/(.*)', StaticHandler);

    console.log('[SanhaiRedirect] 插件已启用');
}

// 定义插件
definePlugin('sanhai-redirect', {
    apply,
});
